﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Foothill.Common;
using Foothill.IoC;
using Foothill.Model;
using Foothill.Service.UserAccount;
using System.Web.Security;

namespace Foothill.Web
{
    public partial class LoginUsingTempPassword : System.Web.UI.Page
    {
        private IAuthenticationService _authenticationService;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            lblLoginError.Visible = false;
        }

        protected void btnLoginClick(object sender, EventArgs e)
        {
            string errorMessage;
            CurrentLoginUser loginUser;
            _authenticationService = IocContainer.GetInstance<IAuthenticationService>();

            AuthenticationResult authResult = _authenticationService.AuthenticateUsingTempPassword(txtEmail.Text.Trim(), txtTempPassword.Text.Trim(), out loginUser, out errorMessage);

            if (authResult.AuthenticateUser)
            {
                // login ok, create authentication ticket and add it to response
                string authTicket = _authenticationService.CreateEncryptedAuthenticationTicket(loginUser.UserAccountInfo.Username);

                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, authTicket));
                Response.Redirect("~/ChangeTempPassword/ChangeTempPassword.aspx");
            }
            else
            {
                // display error
                lblLoginError.Visible = true;
                lblLoginError.Text = errorMessage;
            }
        }
    }
}